<#include "*/component/Tags.ftl"/>
<@Title>
<#if entity.table.containsUnique>
import com.jianxuan.common.core.exception.BaseException;
</#if>
<#if entity.table.containsCombineColumn>
import ${context.projectPackage}.common.utils.ColumnUtils;
</#if>
</@Title>
<@ClassAnnotation>
@Service
@Slf4j
</@ClassAnnotation>
public class ${clazz.name} implements ${clazz.parent.name} {

${'\t'}@Autowired
${'\t'}private ${entity.entityName}Dao ${entity.objectName}Dao;

${'\t'}@Override
${'\t'}public ${entity.entityName}CheckInfoDTO getCheckInfo(${entity.primaryField.type} id) {
${'\t'}${'\t'}${entity.entityName}CheckInfoDTO.QueryCondition queryCondition = new ${entity.entityName}CheckInfoDTO.QueryCondition(id);
${'\t'}${'\t'}return ${entity.objectName}Dao.getOne(queryCondition, ${entity.entityName}CheckInfoDTO.class);
${'\t'}}

${'\t'}@Override
${'\t'}public ListVo<${entity.entityName}ListItemVO> page(${entity.entityName}Query query) {
${'\t'}${'\t'}${entity.entityName}ListItemDTO.QueryCondition queryCondition = ${entity.entityName}CommonMapper.instance.to${entity.entityName}ListItemDTO_QueryCondition(query);
${'\t'}${'\t'}ListVo<${entity.entityName}ListItemDTO> listWithCount = ${entity.objectName}Dao.listWithCount(queryCondition, new ${entity.entityName}ListItemDTO.QueryOrderBy(), query.getPage(), query.getPageSize(), ${entity.entityName}ListItemDTO.class);
${'\t'}${'\t'}return ${entity.entityName}CommonMapper.instance.to${entity.entityName}ListItemVOListVo(listWithCount);
${'\t'}}

${'\t'}@Override
${'\t'}public List<${entity.entityName}ListItemVO> list(${entity.entityName}Query query) {
${'\t'}${'\t'}${entity.entityName}ListItemDTO.QueryCondition queryCondition = ${entity.entityName}CommonMapper.instance.to${entity.entityName}ListItemDTO_QueryCondition(query);
${'\t'}${'\t'}List<${entity.entityName}ListItemDTO> list = ${entity.objectName}Dao.list(queryCondition, new ${entity.entityName}ListItemDTO.QueryOrderBy(), ${entity.entityName}ListItemDTO.class);
${'\t'}${'\t'}return ${entity.entityName}CommonMapper.instance.to${entity.entityName}ListItemVOList(list);
${'\t'}}

${'\t'}@Override
${'\t'}public ${entity.entityName}ProfileVO getProfile(${entity.primaryField.type} id) {
${'\t'}${'\t'}${entity.entityName}ProfileDTO.QueryCondition queryCondition = new ${entity.entityName}ProfileDTO.QueryCondition(id);
${'\t'}${'\t'}${entity.entityName}ProfileDTO profile = ${entity.objectName}Dao.getOne(queryCondition, ${entity.entityName}ProfileDTO.class);
${'\t'}${'\t'}return ${entity.entityName}CommonMapper.instance.to${entity.entityName}ProfileVO(profile);
${'\t'}}
<#if entity.table.containsUnique>
<#list entity.fields as field>
<#if field.column.uniqueKey>

${'\t'}private void verify<#if !field.column.isCombine>${field.capitalizeName}<#elseif field.combineFields?size == 1><#list field.combineFields as relationField>${relationField.capitalizeName}</#list><#else>${field.capitalizeName}</#if>(${entity.primaryField.type} id<#if field.column.uniqueKey><#if !field.column.isCombine>,${field.type} ${field.name}<#elseif field.combineFields?size == 1><#list field.combineFields as relationField>,${relationField.type} ${relationField.name}</#list><#else><#list field.combineFields as relationField>,${relationField.type} ${relationField.name}</#list></#if></#if>){
${'\t'}${'\t'}${entity.entityName}CheckInfoDTO.QueryCondition condition = ${entity.entityName}CheckInfoDTO.QueryCondition.builder().<#if !field.column.isCombine>${field.name}<#elseif field.combineFields?size == 1><#list field.combineFields as relationField>${relationField.name}</#list><#else>${field.name}</#if>(<#if !field.column.isCombine>${field.name}<#elseif field.combineFields?size == 1><#list field.combineFields as relationField>${relationField.name}</#list><#else><#list field.combineFields as relationField><#if relationField_has_next>${relationField.name},<#else>${relationField.name}</#if></#list></#if>).build();
${'\t'}${'\t'}${entity.entityName}CheckInfoDTO target = ${entity.objectName}Dao.getOne(condition, ${entity.entityName}CheckInfoDTO.class);
${'\t'}${'\t'}if (target == null) {
${'\t'}${'\t'}${'\t'}return;
${'\t'}${'\t'}}
${'\t'}${'\t'}if (id==null){
${'\t'}${'\t'}${'\t'}throw new BaseException("${field.column.comment}已存在");
${'\t'}${'\t'}}
${'\t'}${'\t'}if (!target.getId().equals(id)){
${'\t'}${'\t'}${'\t'}throw new BaseException("${field.column.comment}已存在");
${'\t'}${'\t'}}
${'\t'}}
</#if>
</#list>
</#if>

${'\t'}@Override
${'\t'}@Transactional
${'\t'}public void add(${entity.entityName}AddForm form) {
${'\t'}${'\t'}${entity.entityName} addPo = ${entity.entityName}CommonMapper.instance.to${entity.entityName}(form);
${'\t'}${'\t'}Date now = new Date(System.currentTimeMillis());
${'\t'}${'\t'}addPo.setCreateTime(now);
${'\t'}${'\t'}addPo.setUpdateTime(now);
<#list entity.fields as field>
<#if field.column.uniqueKey>
${'\t'}${'\t'}this.verify<#if !field.column.isCombine>${field.capitalizeName}<#elseif field.combineFields?size == 1><#list field.combineFields as relationField>${relationField.capitalizeName}</#list><#else>${field.capitalizeName}</#if>(null<#if field.column.uniqueKey><#if !field.column.isCombine>,form.get${field.capitalizeName}()<#elseif field.combineFields?size == 1><#list field.combineFields as relationField>,form.get${relationField.capitalizeName}()</#list><#else><#list field.combineFields as relationField>,form.get${relationField.capitalizeName}()</#list></#if></#if>);
</#if>
</#list>
<#list entity.fields as field >
<#if field.column.isCombine>
${'\t'}${'\t'}addPo.set${field.capitalizeName}(<#if field.combineFields?size == 1><#list field.combineFields as relationField>ColumnUtils.getDeletableUniqueName(form.get${relationField.capitalizeName}())</#list><#else>ColumnUtils.combineColumns(<#list field.combineFields as relationField><#if relationField_has_next>form.get${relationField.capitalizeName}(),<#else>form.get${relationField.capitalizeName}()</#if></#list>)</#if>);
</#if>
</#list>
${'\t'}${'\t'}${entity.objectName}Dao.save(addPo);
${'\t'}}

${'\t'}@Override
${'\t'}@Transactional
${'\t'}public int update(${entity.entityName}UpdateForm form) {
${'\t'}${'\t'}${entity.entityName}UpdateDTO updateData = ${entity.entityName}CommonMapper.instance.to${entity.entityName}UpdateDTO(form);
<#list entity.fields as field>
<#if field.column.uniqueKey>
${'\t'}${'\t'}this.verify<#if !field.column.isCombine>${field.capitalizeName}<#elseif field.combineFields?size == 1><#list field.combineFields as relationField>${relationField.capitalizeName}</#list><#else>${field.capitalizeName}</#if>(form.getId()<#if field.column.uniqueKey><#if !field.column.isCombine>,form.get${field.capitalizeName}()<#elseif field.combineFields?size == 1><#list field.combineFields as relationField>,form.get${relationField.capitalizeName}()</#list><#else><#list field.combineFields as relationField>,form.get${relationField.capitalizeName}()</#list></#if></#if>);
</#if>
</#list>
<#list entity.fields as field >
<#if field.column.isCombine>
${'\t'}${'\t'}updateData.set${field.capitalizeName}(<#if field.combineFields?size == 1><#list field.combineFields as relationField>ColumnUtils.getDeletableUniqueName(form.get${relationField.capitalizeName}())</#list><#else>ColumnUtils.combineColumns(<#list field.combineFields as relationField><#if relationField_has_next>form.get${relationField.capitalizeName}(),<#else>form.get${relationField.capitalizeName}()</#if></#list>)</#if>);
</#if>
</#list>
${'\t'}${'\t'}${entity.entityName}UpdateDTO.UpdateCondition updateCondition = new ${entity.entityName}UpdateDTO.UpdateCondition(form.getId());
${'\t'}${'\t'}return ${entity.objectName}Dao.update(updateData, updateCondition);
${'\t'}}

${'\t'}@Override
${'\t'}@Transactional
${'\t'}public int delete(${entity.primaryField.type} id) {
${'\t'}${'\t'}${entity.entityName}DeleteDTO.DeleteCondition deleteCondition = new ${entity.entityName}DeleteDTO.DeleteCondition(id);
${'\t'}${'\t'}return ${entity.objectName}Dao.update(new ${entity.entityName}DeleteDTO(new Date(System.currentTimeMillis())), deleteCondition);
${'\t'}}

}
